package com.runshu.newsmartark.base;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;


import com.naz.serial.port.SerialPort;
import com.runshu.newsmartark.event.BindEventBus;
import com.runshu.newsmartark.event.EventBusUtils;
import com.runshu.newsmartark.networkstate.NetworkBus;
import com.runshu.newsmartark.util.FinishActivityManager;
import com.runshu.newsmartark.util.MacUtils;
import com.runshu.newsmartark.util.common.SharedPreferencesUtils;
import com.runshu.newsmartark.util.y.nativelog.NLogUtil;
import com.umeng.analytics.MobclickAgent;

import java.io.File;
import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.runshu.newsmartark.common.ConstantUrl.Share_hasAuth;
import static com.runshu.newsmartark.common.ConstantUrl.Share_isAdmin;


public abstract class BaseActivity extends AppCompatActivity {
    public Context context;
    private Unbinder mButterBind;
    protected RelativeLayout container;
    public Bundle saveInstanceState;
    protected final String TAG = this.getClass().getSimpleName();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.saveInstanceState = savedInstanceState;
        context = this;
        FinishActivityManager.getManager().addActivity(this);
        container = new RelativeLayout(context);
        container.addView(LayoutInflater.from(context).inflate(getLayoutId(), null, false), new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        setContentView(container, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mButterBind = ButterKnife.bind(this);
        // 若使用BindEventBus注解，则绑定EventBus
        if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
            EventBusUtils.register(this);
        }
        initView();
        initData();
        bindListener();
        NLogUtil.i( NLogUtil.INFO_RestartAPP_APPINFO,"version:"+ MacUtils.getLocalVersion( this )  );

    }

    /**
     * 设置布局id
     *
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * 初始控件 做一些findviewbyId
     */
    protected abstract void initView() ;

    /**
     * 设置数据的显示
     */
    protected abstract void initData();

    /**
     * 设置点击事件
     */
    protected abstract void bindListener();


    /**
     * 权限检查
     *
     * @param neededPermissions 需要的权限
     * @return 是否全部被允许
     */
    protected boolean checkPermissions(String[] neededPermissions) {
        if (neededPermissions == null || neededPermissions.length == 0) {
            return true;
        }
        boolean allGranted = true;
        for (String neededPermission : neededPermissions) {
            allGranted &= ContextCompat.checkSelfPermission(this, neededPermission) == PackageManager.PERMISSION_GRANTED;
        }
        return allGranted;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean isAllGranted = true;
        for (int grantResult : grantResults) {
            isAllGranted &= (grantResult == PackageManager.PERMISSION_GRANTED);
        }
        afterRequestPermission(requestCode, isAllGranted);
    }

    /**
     * 请求权限的回调
     *
     * @param requestCode  请求码
     * @param isAllGranted 是否全部被同意
     */
    protected abstract void afterRequestPermission(int requestCode, boolean isAllGranted);

    protected void showToast(String s) {
        Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
    }

    protected void showLongToast(String s) {
        Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FinishActivityManager.getManager().finishActivity(this);
        mButterBind.unbind();
        // 若使用BindEventBus注解，则解绑定EventBus
        if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
            EventBusUtils.unregister(this);
        }
//        HttpExecutor.cancleAllRequest();
    }

    /**
     * 是否身份认证过了
     *
     * @return
     */
    public boolean hasAuth() {

        return (boolean) SharedPreferencesUtils.get(context, Share_hasAuth, false);

    }
    public void exitLogin(){
        SharedPreferencesUtils.put(context, Share_hasAuth, false);
        MyApplication.application.removeUserInfo();
    }

    public void loginSuccess(){
        SharedPreferencesUtils.put(context, Share_hasAuth, true);
    }
    /**
     * 是否是管理
     *
     * @return
     */
    public void setisAdmin() {
        SharedPreferencesUtils.put(context,Share_isAdmin,true);
    }

    public boolean isAdmin(){
        return (boolean) SharedPreferencesUtils.get(context,Share_isAdmin,false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        NetworkBus.getDefault().register(BaseActivity.this);

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onResume(this);
        NetworkBus.getDefault().unregister(BaseActivity.this);

    }
}
